#include <iostream>
#include <vector>

using namespace std;

class Solution
{
public:
    int countHighestScoreNodes(vector<int> &parents)
    {
        vector<pair<int, int>> children(parents.size(), {-1, -1});
        for (int i = 0; i < parents.size(); i++)
        {
            if (parents[i] == -1)
                continue;
            auto &p = children[parents[i]];
            if (p.first == -1)
                p.first = i;
            else
                p.second = i;
        }

        vector<int> count(parents.size(), -1);
        dfs(0, children, count);

        long long max_score = 0;
        int c = 0;
        for (int i = 0; i < parents.size(); i++)
        {
            long long socre = 1;
            // parent
            if (parents[i] != -1)
                socre *= (count[0] - count[i]);
            // children
            if (children[i].first != -1)
                socre *= (count[children[i].first]);
            if (children[i].second != -1)
                socre *= (count[children[i].second]);
            if (socre > max_score)
            {
                max_score = socre;
                c = 1;
            }
            else if (socre == max_score)
                c++;
        }
        return c;
    }

    int dfs(int root, vector<pair<int, int>> &children, vector<int> &count)
    {
        if (count[root] != -1)
            return count[root];
        int c = 1;
        if (children[root].first != -1)
            c += dfs(children[root].first, children, count);
        if (children[root].second != -1)
            c += dfs(children[root].second, children, count);
        count[root] = c;
        return c;
    }
};

int main()
{
    cout << "Code2049" << endl;
    Solution s;
    vector<int> parent = {-1, 2, 0, 2, 0};
    cout << s.countHighestScoreNodes(parent) << endl;
    return 0;
}